import datetime

from sqlalchemy import String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.infrastructure.db.models.base import Base
from src.infrastructure.db.models.m2ms import server_categories, sites_categories


class Site(Base):
    __tablename__ = "site"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(50))
    categories = relationship("Category", secondary=sites_categories, back_populates="sites")
    orders = relationship("Order", back_populates="site")

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"<Site №{self.id} {self.name}>"


class Category(Base):
    __tablename__ = "category"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(50), nullable=False)
    name_eng: Mapped[str] = mapped_column(String(50), nullable=False)
    sites = relationship("Site", secondary=sites_categories, back_populates="categories")
    servers = relationship("Server", secondary=server_categories, back_populates="categories")
    orders = relationship("Order", back_populates="category")
    price_4h: Mapped[float] = mapped_column(nullable=False)
    price_1d: Mapped[float] = mapped_column(nullable=False)
    price_1w: Mapped[float] = mapped_column(nullable=False)
    price_2w: Mapped[float] = mapped_column(nullable=False)
    price_1m: Mapped[float] = mapped_column(nullable=False)
    created_at: Mapped[datetime.datetime] = mapped_column(server_default=func.now())

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"<Category №{self.id} {self.name}>"
